package cn.org.timespace.ons.dao.base.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.ons.action.base.TypeAuditModel;
import cn.org.timespace.ons.dao.TSBaseDAO;
import cn.org.timespace.ons.dao.base.TypeAuditDao;
import cn.org.timespace.ons.util.datatime.DateTimeUtils;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.vo.base.MachinesApply;
import cn.org.timespace.ons.vo.base.TypeAudit;



public class TypeAuditDaoImpl  extends TSBaseDAO implements TypeAuditDao {

	@SuppressWarnings("static-access")
	@Override
	public List<Object> findByCondition(TypeAuditModel model, Pager p) {

		log.debug("TypeAuditDaoImpl finding all findByCondition instances");
			
			Session session = null;
			Query query = null;
			try {
				StringBuffer queryString = new StringBuffer("select eu1.compName,eu2.compName,u.name,m.name,t.theId,t.status,t.sentTime " +
						" from TypeAudit t,AppleServer u,UserMachineType m,EfUser eu1,EfUser eu2 " +
						" where 1=1 " +
						" and u.asid=t.theIdapply and t.theIdmachine=m.theIdMachine ");
				
				if (!StringUtils.isEmpty(model.getEndUserName()) && !StringUtils.isEmpty(model.getStartUserName())) {
					queryString.append(" and t.endUserName ='" + model.getEndUserName()
							+ "' and t.endUserName = eu2.userAccount " 
							+ " and t.startUserName ='" + model.getStartUserName() + "' " 
							+ " and t.startUserName =eu1.userAccount ");
				}
				//如果startUserName不为空 则为我的申请
				else if(!StringUtils.isEmpty(model.getStartUserName()) && StringUtils.isEmpty(model.getEndUserName())) {
					queryString.append(" and t.startUserName ='" + model.getStartUserName()+ "'"+
							" and t.startUserName =eu1.userAccount " +
							" and t.endUserName = eu2.userAccount " +
							//" and t.status <> 3");//查询条件里会查不出已取消的申请
							"");
				}
				//如果endUserName不为空 则为我的审核
				else if (!StringUtils.isEmpty(model.getEndUserName()) && StringUtils.isEmpty(model.getStartUserName())) {
					queryString.append(" and t.endUserName ='" + model.getEndUserName()+"'"
							+ " and t.startUserName =eu1.userAccount " +
							" and t.endUserName = eu2.userAccount ");
				}
				
				//审核状态
				if(model.getStatus()!=null){
					queryString.append(" and t.status = "+model.getStatus());
					
				}
				//公司名称
				if(model.getCompName()!=null&&model.getCompName().length()>0){
					queryString.append(" and eu2.compName like '%"+model.getCompName()+"%'");
					
				}
				//应用名称
				if(model.getApply()!=null&&model.getApply().length()>0){
					queryString.append(" and u.name like '%"+model.getApply()+"%'");
					
				}
				//终端类型
				if(model.getTheIdmachine()!=null&&model.getTheIdmachine().length()>0){
					queryString.append(" and t.theIdmachine = '"+model.getTheIdmachine()+"'");
					
				}
				queryString.append(" order by t.sentTime desc");
				session = getSessionFactory().openSession();
				query = session.createQuery(queryString.toString());
				List<Object> resList = new ArrayList();
				resList.add(query.list().size());
				setQueryPager(query, p);
				
				List list = new ArrayList();
				for(int i = 0;i < query.list().size();i++){
					Object[] obj=(Object[])query.list().get(i);
					DateTimeUtils dateTimeUtils=new DateTimeUtils();
					 
					TypeAudit typeAudit = new TypeAudit();
					typeAudit.setStartUserName((obj[0].toString()));
					typeAudit.setEndUserName((obj[1].toString()));
					typeAudit.setTheIdapply((obj[2].toString()));
					typeAudit.setTheIdmachine((obj[3].toString()));
					typeAudit.setTheId(((obj[4].toString())));
					typeAudit.setStatus((((Integer.parseInt(obj[5].toString())))));
					Date dateTimes=dateTimeUtils.parseDate(obj[6].toString(), dateTimeUtils.PATTERN_DATETIME);
					typeAudit.setSentTime(dateTimes);
					
					list.add(typeAudit);
				}
				resList.add(list);
				return resList;
				
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}finally{
				closeSessionAndQuery(session, query);
			}
		
	}

	@Override
	public TypeAudit findById(String theId) {
		
		log.debug("getting TypeAudit instance with id: " + theId);
		try {
			TypeAudit instance = (TypeAudit) getHibernateTemplate().get(
					"cn.org.timespace.ons.vo.base.TypeAudit", theId);
			return instance;
		} catch (RuntimeException re) {
			
			log.error("get failed", re);
			throw re;
		}
	
	}

	@Override
	public void update(TypeAudit instance) {

		log.debug("updateing TypeAudit instance");
		try {
			
			getHibernateTemplate().update(instance);
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
		
	}
	
	public boolean save(TypeAudit instance) {

		boolean flag = false;
		log.debug("save TypeAudit instance");
		try {
			
			getHibernateTemplate().save(instance);
			flag = true;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return flag;
		
	}
	@Override
	public List<TypeAudit> findByCondition(TypeAuditModel model) {
		

		// TODO Auto-generated method stub
		StringBuffer queryString = new StringBuffer("from TypeAudit t where 1=1");
		//根据状态
		if (!StringUtils.isEmpty(model.getStatus()+"")) {
			queryString.append(" and t.status = " + model.getStatus()
					+ " "
					);
		}
		
		//根据发起人查询
		if (!StringUtils.isEmpty(model.getStartUserName())) {
			queryString.append(" and t.startUserName = '" + model.getStartUserName()
					+ "' "
					);
		}
		//审核人
		if (!StringUtils.isEmpty(model.getEndUserName())) {
			queryString.append(" and t.endUserName = '" + model.getEndUserName()
					+ "' "
					);
		}
		
		//根据应用平台查询
		if(!StringUtils.isEmpty(model.getTheIdapply())){
			queryString.append(" and t.theIdapply ='" + model.getTheIdapply() +"'");
		}
		
		//根据信息终端类型查询
		if(!StringUtils.isEmpty(model.getTheIdmachine())){
			queryString.append(" and t.theIdmachine ='" + model.getTheIdmachine() +"'");
		}
		
		
		
		queryString.append(" order by t.theId desc");
		return getHibernateTemplate().find(queryString.toString());

	}

}
